﻿/**********************************************
单证库位维护（Tree）
***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Components;
using Ebada.Client;
using Ebada.Platform.Model;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Document
{
    /// <summary>
    /// 单证库位管理
    /// </summary>
    public partial class UCDocumentPositionTree : DevExpress.XtraEditors.XtraUserControl {

        TreeViewOperation<DocumentPosition> treeViewOperator;
        SortableSearchableBindingList<mOrg> m_OrgID = new SortableSearchableBindingList<mOrg>();
        public TreeViewOperation<DocumentPosition> TreeViewOperator {
            get { return treeViewOperator; }
            set { treeViewOperator = value; }
        }
        public event SendDataEventHandler<DocumentPosition> FocusedNodeChanged;
        public event SendDataEventHandler<DocumentPosition> AfterAdd;
        public event SendDataEventHandler<DocumentPosition> AfterEdit;
        public event SendDataEventHandler<DocumentPosition> AfterDelete;
        public UCDocumentPositionTree() {
            InitializeComponent();
            treeViewOperator = new TreeViewOperation<DocumentPosition>(treeList1,barManager1,new frmDocumentPositionEdit());
           
            treeViewOperator.CreatingObjectEvent += treeViewOperator_CreatingObject;
            treeViewOperator.BeforeAdd += new ObjectOperationEventHandler<DocumentPosition>(treeViewOperator_BeforeAdd);
            treeViewOperator.AfterAdd += treeViewOperator_AfterAdd;
            treeViewOperator.BeforeInsert += treeViewOperator_BeforeInsert;
            treeViewOperator.BeforeUpdate += treeViewOperator_BeforeUpdate;
            treeViewOperator.AfterEdit +=treeViewOperator_AfterEdit;
            treeViewOperator.AfterDelete +=treeViewOperator_AfterDelete;
            treeList1.FocusedNodeChanged += treeList1_FocusedNodeChanged;
            ShowColumnsTitle();
        }
        public void ShowColumnsTitle()
        {
            for (int i = 0; i < treeList1.Columns.Count; i++)
            {
                treeList1.Columns[i].Visible = false;
            }
            int m = 0;
            treeList1.Columns["PositionName"].Caption = "位置名称";
            treeList1.Columns["PositionName"].VisibleIndex = m++;
            treeList1.Columns["PositionLevel"].Caption = "位置级别";
            treeList1.Columns["PositionLevel"].VisibleIndex = m++;
            treeList1.Columns["Org_ID"].Caption = "关联单位";
            treeList1.Columns["Org_ID"].VisibleIndex = m++;
            //treeList1.Columns["CreateUserName"].Caption = "创建人";
            //treeList1.Columns["CreateUserName"].VisibleIndex = m++;
            //treeList1.Columns["LastEditUserName"].Caption = "修改人";
            //treeList1.Columns["LastEditUserName"].VisibleIndex = m++;
            //treeList1.Columns["LastEditTime"].Caption = "修改时间";
            //treeList1.Columns["LastEditTime"].VisibleIndex = m++;
           
        }
        void treeViewOperator_BeforeAdd(object render, ObjectOperationEventArgs<DocumentPosition> e)
        {
            string parentID = e.Value.ParentID;
            if (parentID!="0")
            {
                e.Value.PositionLevel = Convert.ToInt32(treeList1.FindNodeByKeyID(parentID)["PositionLevel"]) + 1;

            }
          
           
        }

        void treeViewOperator_BeforeUpdate(object render, ObjectOperationEventArgs<DocumentPosition> e)
        {//记录修改保存前发生
            //e.Value.LastEditTime = DateTime.Now;
        }

        void treeViewOperator_BeforeInsert(object render, ObjectOperationEventArgs<DocumentPosition> e)
        {//新记录保存前发生
            //e.Value.CreatedDate = DateTime.Now;
        }

        void treeViewOperator_AfterDelete(DocumentPosition newobj) {
            if (AfterDelete != null)
                AfterDelete(treeList1, newobj);
        }

        void treeViewOperator_AfterEdit(DocumentPosition newobj) {
            if (AfterAdd != null)
                AfterEdit(treeList1, newobj);
        }

        void treeViewOperator_AfterAdd(DocumentPosition newobj) {
            if (AfterAdd != null)
                AfterAdd(treeList1, newobj);
           
        }

        void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e) {
            if (FocusedNodeChanged != null)
                FocusedNodeChanged(treeList1,treeList1.GetDataRecordByNode(e.Node) as DocumentPosition);
        }

        void treeViewOperator_CreatingObject(DocumentPosition newobj) {
        }
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            treeList1.KeyFieldName = "DocumentPositionID";
            treeList1.ParentFieldName = "ParentID";
            if (this.Site == null)
                InitData();
        }
       
        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitData() {
            treeViewOperator.RefreshData("order by parentid,DocumentPositionID");
           
           // treeList1.Columns["Org_ID"].Visible = false;
            //如果需要隐藏列和工具栏
            //请在引用窗体调用以下方法
            //hidecolum();
            //hidebuttom();
            Init_Org();
        }
        public void Init_Org()
        {
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit comboBox = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            treeList1.Columns["Org_ID"].ColumnEdit = comboBox;
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            comboBox.DataSource = org_list;
            comboBox.DisplayMember = "cnName";
            comboBox.ValueMember = "Org_ID";
           
        }
    
        public void hidecolum()
        {
            for (int i = treeList1.Columns.Count-1; i >= treeList1.Columns.Count-2; i--)
            {
               treeList1.Columns[i].Visible = false;
            }
            treeList1.Columns["Org_ID"].Visible = false;
        }
        public void hidebuttom()
        {
            this.btAdd.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btAdd2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btClose.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btEdit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btExport.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btDelete.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btFind.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btRefresh.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            
        }
        public void hideBar()
        {
            this.bar1.Visible = false;
            this.bar3.Visible = false;
        }
        public void FindTree(string sqlwhere)
        {
            treeViewOperator.RefreshData(sqlwhere);
        }
    }
}
